home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: MegaDisc / MegaDisc 27 (1992-03)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2).zip / MegaDisc 27 (1992-03)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2).adf / Programming / Integer_Math / LongInts.i < prev    next >
Text File  |  1992-03-30  |  8KB  |  329 lines

  1.     IFND    LONGINTSVER
  2.  
  3.     ;-------------------------------------------------------------------
  4.     ; Created by Peter Thompson
  5.     ; Multiply/square routines written on New Year's Day 1992
  6.     ; & debugged on 2nd January, when I was feeling better....
  7.     ; Division macros written & partially debugged on 2nd Jan.
  8.     ; Further testing and debugging on 10th Jan.
  9.     ;-------------------------------------------------------------------
  10.     ; $MUSIC="Vince Jones/It all ends up in tears, Enya/Watermark, Paul
  11.     ;         Simon/Graceland, Clannad/Fuaim, Paul Simon/The Rhythm of
  12.     ;         the Saints, Greg X. Volz/No Room in the Middle, Greg X.
  13.     ;      Volz/The Exodus"
  14.     ;-------------------------------------------------------------------
  15.     ;  All numbers are integers.
  16.     ;  Only registers containing results, and/or d1, are altered by
  17.     ; these macros.
  18.     ; Flag    State after any multiply/square macro
  19.     ; X    undefined.
  20.     ; N    Set if result is negative, clear otherwise.
  21.     ; Z    Set if result is zero, clear otherwise.
  22.     ; V    if set, an overflow HAS taken place;
  23.     ;    if clear, an overflow MAY have taken place.
  24.     ; C    undefined.
  25.     ;  None of these routines can be trusted to return a correct
  26.     ; overflow flag, unless specified by name to do so. It is your
  27.     ; responsiblity to make sure your numbers are small enoungh.
  28.     ;-------------------------------------------------------------------
  29.  
  30. LONGINTSVER    EQU    100
  31.  
  32.     ;- MUL3264 macro - 32x32->64 bit multiply.
  33.     ;- Input:    d0 contains a 32-bit number (treated as unsigned).
  34.     ;-        d1 contains a 32-bit number (treated as unsigned).
  35.     ;- Output:    d0 and d1 contain the product of the inputs in the
  36.     ;        order least to most significant longword.
  37.  
  38. MUL3264:    MACRO        ; d0 d1 d2 d3 d4
  39.     movem.l    d2-d4,-(SP)    ; ab cd -- -- --
  40.     move.l    d1,d2        ; ab cd cd -- --
  41.     swap    d1        ; ab dc cd -- --
  42.     move.l    d1,d3        ; ab dc cd dc --
  43.     move.l    d0,d4        ; ab dc cd dc ab
  44.     swap    d4        ; ab dc cd dc ba
  45.     mulu    d4,d1        ; ab AC cd dc ba
  46.     mulu    d0,d3        ; ab AC cd BC ba
  47.     mulu    d2,d0        ; BD AC cd BC ba
  48.     mulu    d4,d2        ; BD AC AD BC ba
  49.     moveq    #0,d4        ; BD AC AD BC 00
  50.     add.l    d2,d3        ; BD AC AD 12 00
  51.     addx.w    d4,d4        ; BD AC AD 12 0+
  52.     swap    d3        ; BD AC AD 21 0+
  53.     swap    d4        ; BD AC AD 21 +0
  54.     move.w    d3,d4        ; BD AC AD 21 +1
  55.     clr.w    d3        ; BD AC AD 20 +1
  56.     add.l    d3,d0        ; ++ AC AD 20 +1
  57.     addx.l    d4,d1        ; ++ ++ AD 20 +1
  58.     movem.l    (SP)+,d2-d4    ; ++ ++ -- -- --
  59.     ENDM
  60.  
  61.     ;- SQR3264 macro - 32->64 bit (unsigned) square.
  62.     ;- Input:    d0 contains a 32-bit number (treated as unsigned).
  63.     ;- Output:    d0 and d1 contain the square of the input in the
  64.     ;-        order least to most significant longword.
  65.  
  66. SQR3264:    MACRO        ; d0 d1 d2 d3
  67.     movem.l    d2-d3,-(SP)    ; ab -- -- --
  68.     moveq    #0,d1
  69.     move.l    d0,d2        
  70.     swap    d2        ; ab 00 ba --
  71.     move.l    d2,d3        ; ab 00 ba ba 
  72.     mulu    d0,d2        ; ab 00 AB ba
  73.     mulu    d0,d0        ; BB 00 AB ba
  74.     mulu    d3,d3        ; BB 00 AB AA
  75.     swap    d2
  76.     move.w    d2,d1
  77.     clr.w    d2        ; BB A0 B0 AA
  78.     add.l    d2,d2
  79.     addx.l    d1,d1        ; double AB0
  80.     add.l    d2,d0
  81.     addx.l    d3,d1        ; add both parts
  82.     movem.l    (SP)+,d2-d3
  83.     ENDM
  84.  
  85.     ;- MUL64128 macro - 64x64->128 bit multiply.
  86.     ;- Input:    d0 and d1 contain a 64-bit number (treated as
  87.     ;-        unsigned) in the order least to most significant
  88.     ;-        longword.
  89.     ;-        d2 and d3 contain a 64-bit number (treated as
  90.     ;-        unsigned) in the order least to most significant
  91.     ;-        longword.
  92.     ;- Output:    d0,d1,d2 and d3 contain the product of the inputs
  93.     ;-        in the order least to most significant longword.
  94.     
  95. MUL64128:    MACRO
  96.                 ; d0 d1 d2 d3 d4 d5 d6 d7
  97.                 ; a0 a1 a2 a3 a4 a5 a6
  98.     movem.l    d4-d7/a0-a6,-(SP)
  99.                 ; cd ab 34 12 -- -- -- --
  100.     move.l    d2,a4
  101.     move.l    d2,d5
  102.     swap    d5
  103.     move.l    d5,a3        ; cd ab 34 12 -- 43 -- --
  104.                 ; -- -- -- 43 34 -- --
  105.     move.l    d3,a2
  106.     move.l    d3,d6
  107.     swap    d6
  108.     move.l    d6,a1        ; cd ab 34 12 -- 43 21 --
  109.                 ; -- 21 12 43 34 -- --
  110.     mulu    d0,d2        ;make D4 (=4 done)
  111.     mulu    d0,d3        ;make D2
  112.     mulu    d0,d5        ;make D3
  113.     mulu    d0,d6        ;make D1
  114.                 ; cd ab D4 D2 -- D3 D1 --
  115.                 ; -- 21 12 43 34 -- --        
  116.     move.l    d2,a0
  117.     swap    d0
  118.     moveq    #0,d7
  119.     move.l    a4,d2
  120.     mulu    d0,d2        ;make C4 (/4 done)
  121.     add.l    d2,d5        ; dc ab C4 D2 -- /4 D1 00
  122.                 ; D4 21 12 43 34 -- --
  123.     move.l    a2,d2
  124.     mulu    d0,d2        ;make C2
  125.     addx.l    d2,d6        ; dc ab C2 D2 -- /4 /3 00
  126.                 ; D4 21 12 43 34 -- --
  127.     move.l    a1,d2
  128.     mulu    d1,d2        ;make B1
  129.     addx.l    d2,d7
  130.     move.l    d5,a6        ; dc ab B1 D2 -- /4 /3 /2
  131.                 ; D4 21 12 43 34 -- /4
  132.     moveq    #0,d5
  133.     move.l    a1,d4
  134.     mulu    d0,d4        ;make C1
  135.     move.l    a3,d2
  136.     mulu    d0,d2        ;make C3
  137.     add.l    d2,d3        ; dc ab C3 =3 C1 00 /3 /2
  138.                 ; D4 21 12 43 34 -- /4
  139.     move.l    d1,d0
  140.     swap    d0
  141.     move.l    a2,d2
  142.     mulu    d1,d2        ;make B2
  143.     addx.l    d2,d4        ; ba ab B2 =3 =2 00 /3 /2
  144.                 ; D4 21 12 43 34 -- /4
  145.     move.l    a1,d2
  146.     mulu    d0,d2        ;make A1
  147.     addx.l    d2,d5        ; ba ab B2 =3 =2 =1 /3 /2
  148.                 ; D4 21 12 43 34 -- /4
  149.     move.l    a4,d2
  150.     mulu    d1,d2        ;make B4 (=3 done)
  151.     add.l    d2,d3        ; ba ab B4 =3 =2 =1 /3 /2
  152.                 ; D4 21 12 43 34 -- /4
  153.     move.l    d3,a5
  154.     move.l    a3,d2
  155.     mulu    d2,d1        ;make B3
  156.     moveq    #0,d3
  157.     move.l    a3,d2
  158.     mulu    d0,d2        ;make A3 (=2 done)
  159.     addx.l    d2,d4        ;(=1 done)
  160.     addx.l    d3,d5        ; ba B3 A3 00 =2 =1 /3 /2
  161.                 ; D4 21 12 43 34 =3 /4
  162.     add.l    d1,d6
  163.     move.l    a2,d2
  164.     mulu    d0,d2        ;make A2
  165.     addx.l    d2,d7
  166.     addx.w    d3,d3        ; ba B3 A2 /1 =2 =1 /3 /2
  167.                 ; D4 21 12 43 34 =3 /4
  168.     moveq    #0,d2
  169.     move.l    a4,d1
  170.     mulu    d1,d0        ;make A4
  171.     add.l    d0,d6
  172.     addx.l    d2,d7
  173.     addx.w    d2,d3        ; A4 34 00 /1 =2 =1 /3 /2
  174.                 ; D4 21 12 43 34 =3 /4
  175.     move.l    a6,d0
  176.     move.l    a5,d1
  177.     swap    d3
  178.     swap    d7
  179.     move.w    d7,d3
  180.     swap    d6
  181.     move.w    d6,d7
  182.     swap    d0
  183.     move.w    d0,d6
  184.     clr.w    d0        ; 40 =3 00 1/ =2 =1 3/ 2/
  185.                 ; D4 21 12 43 34 =3 /4
  186.     add.l    a0,d0
  187.     addx.l    d6,d1
  188.     move.l    d4,d2
  189.     addx.l    d7,d2
  190.     addx.l    d5,d3        ; ++ ++ ++ ++ =2 =1 3/ 2/
  191.                 ; D4 21 12 43 34 =3 /4
  192.     movem.l    (SP)+,d4-d7/a0-a6
  193.     ENDM
  194.  
  195.     ;- SQR64128 macro - 64->128 bit (unsigned) square.
  196.     ;- Input:    d0 and d1 contain a 64-bit number (treated as
  197.     ;-        unsigned) in the order least to most significant
  198.     ;-        longword.
  199.     ;- Output:    d0,d1,d2 and d3 contain the square of the input in
  200.     ;-        the order least to most significant longword.
  201.  
  202. SQR64128:    MACRO        ; d0 d1 d2 d3 d4 d5 d6 d7
  203.                 ; a0 a1 a2 a3
  204.     movem.l    d4-d7/a0-a3,-(SP)
  205.                 ; cd ab -- -- -- -- -- --
  206.     move.l    d0,d6
  207.     move.l    d1,d7
  208.     swap    d6
  209.     swap    d7
  210.     move.l    d0,a0
  211.     move.l    d1,a1        ; cd ab -- -- -- -- dc ba
  212.                 ; cd ab -- --
  213.     mulu    d0,d1        ;make BD
  214.     move.l    d1,a2
  215.     mulu    d0,d0        ;make DD
  216.     move.l    d0,a3        ; DD BD -- -- -- -- dc ba
  217.                 ; cd ab BD DD
  218.     moveq    #0,d2
  219.     move.l    a0,d0
  220.     mulu    d6,d0        ;make CD
  221.     move.l    a1,d1
  222.     mulu    d6,d1        ;make BC
  223.     move.l    a0,d3
  224.     mulu    d7,d3        ;make AD
  225.     add.l    d3,d1
  226.     move.l    a1,d3
  227.     mulu    d7,d3        ;make AB
  228.     addx.l    d3,d2        ; /3 /2 /1 AB -- -- dc ba
  229.                 ; cd ab BD DD
  230.     moveq    #0,d3
  231.     swap    d2
  232.     move.w    d2,d3
  233.     swap    d1
  234.     move.w    d1,d2
  235.     swap    d0
  236.     move.w    d0,d1
  237.     clr.w    d0        ; 0/ 3/ 2/ 10 -- -- dc ba
  238.                 ; cd ab BD DD
  239.     moveq    #0,d4
  240.     move.l    d6,d5
  241.     mulu    d7,d5        ;make AC
  242.     add.l    a2,d1
  243.     addx.l    d5,d2
  244.     addx.l    d4,d3
  245.     add.l    d0,d0        ;double
  246.     addx.l    d1,d1
  247.     addx.l    d2,d2
  248.     addx.l    d3,d3        ; =4 =3 =2 =1 00 AC dc ba
  249.                 ; cd ab BD DD
  250.     mulu    d6,d6        ;make CC
  251.     move.l    a1,d5
  252.     mulu    d5,d5        ;make BB
  253.     mulu    d7,d7        ;make AA
  254.     add.l    a3,d0        ; =4 =3 =2 =1 00 BB CC AA
  255.                 ; cd ab BD DD
  256.     addx.l    d6,d1
  257.     addx.l    d5,d2
  258.     addx.l    d7,d3
  259.     movem.l    (SP)+,d4-d7/a0-a3
  260.     ENDM
  261.  
  262.     ;- DIV6331 macro - 63/31 bit division.
  263.     ;- Note: use UDIV64 (in Int64.i) for a 64/32 bit division.
  264.     ;- Input:    d0 and d1 contain a 63-bit (positive) dividend in
  265.     ;-        the order least to most significant longword.
  266.     ;-        d2 contains a 31-bit (positive) divisor.
  267.     ;- Output:    d0 contains a 32-bit (unsigned) quotient.
  268.     ;-         d1 contains a 31-bit (positive) remainder.
  269.  
  270. DIV6331:    MACRO
  271.     movem.l    d2-d4,-(SP)
  272.     moveq    #0,d3
  273.     moveq    #63,d4
  274.     lsl.l    #1,d0
  275.     roxl.l    #1,d1
  276.     roxl.l    #1,d3        ;d6432a:
  277.     sub.l    d2,d3
  278.     bcc.S    *+4        ;->d6432b
  279.     add.l    d2,d3
  280.     roxl.l    #1,d0        ;d6432b:
  281.     roxl.l    #1,d1
  282.     dbra    d4,*-$C        ;->d6432a
  283.     not.l    d0
  284.     move.l    d3,d1
  285.     movem.l    (SP)+,d2-d4
  286.     ENDM
  287.  
  288.     ;- DIV12763 macro - 127/63 bit division.
  289.     ;- Input:    d0,d1,d2 and d3 contain a 127-bit (positive)
  290.     ;-        dividend in the order least to most significant
  291.     ;-        longword.
  292.     ;-        d4 and d5 contain a 63-bit (positive) divisor in
  293.     ;-        the order least to most significant longword.
  294.     ;- Output:    d0 and d1 contain a 64-bit (unsigned) quotient in
  295.     ;-        the order least to most significant longword.
  296.     ;-         d2 and d3 contain a 63-bit (positive) remainder in
  297.     ;-        the order least to most significant longword.
  298.  
  299. DIV12763:    MACRO
  300.     movem.l    d4-d7/a0,-(SP)
  301.     moveq    #0,d6
  302.     moveq    #0,d7
  303.     move.l    d4,a0
  304.     moveq    #127,d4
  305.     lsl.l    #1,d0
  306.     roxl.l    #1,d1
  307.     roxl.l    #1,d2
  308.     roxl.l    #1,d3
  309.     roxl.l    #1,d6        ;d12763a:
  310.     roxl.l    #1,d7
  311.     sub.l    a0,d6
  312.     subx.l    d5,d7
  313.     bcc.S    *+6        ;->d12763b
  314.     add.l    a0,d6
  315.     addx.l    d5,d7
  316.     roxl.l    #1,d0        ;d12763b:
  317.     roxl.l    #1,d1    
  318.     roxl.l    #1,d2
  319.     roxl.l    #1,d3
  320.     dbra    d4,*-$16    ;->d12763a
  321.     not.l    d0
  322.     not.l    d1
  323.     move.l    d6,d2
  324.     move.l    d7,d3
  325.     movem.l    (SP)+,d4-d7/a0
  326.     ENDM
  327.  
  328.     ENDC
  329.